package psa.deadlinks;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.client.ClientProtocolException;
import org.unt.url.Page;
import org.unt.url.Site;

public class FindDeadLinks {

	static Set<String> processedPages = new HashSet<String>();
	static List<String> results = new ArrayList<String>();

	Map<String, Page> map = new HashMap<String, Page>();

	public static void main(String[] args) throws ClientProtocolException, IOException {

		Page.site = new Site("jip.inetpsa.com", 80, "http");
		checkChild(null, "/fr/index.html");

		for (String line : results) {
			System.out.println(line);
		}
	}

	private static void checkChild(String parent, String child) throws ClientProtocolException, IOException {

		if (processedPages.contains(child.toLowerCase())) {
			return;
		}

		processedPages.add(child.toLowerCase());

		Page page = new Page(parent, child);

		results.add(parent + "\t" + child + "\t" + page.exist());
		System.out.println(parent + "\t" + child + "\t" + page.exist());

		if (!page.exist()) {
			return;
		}

		if (page.getJipChildren().size() > 0) {

			for (String grandSon : page.getJipChildren()) {
				checkChild(child, grandSon);
			}
		}

		// for (String noJipChild : page.getNoJipChildren()) {
		// checkNoJipChild(noJipChild);
		// }
	}

	// private static void checkNoJipChild(String noJipChild) throws ParseException, IOException {
	// Page page = new Page(noJipChild);
	// if (!page.exist())
	// System.out.println(noJipChild + "\tPAGE NOT EXIST");
	// }
}
